#include "stdafx.h"

// Encode-Decode McAfee Algorythms
// "Cipher table"

const char DAT_CopyRight[50]="Copyright (c) Network Associates Inc.\x1A\0Olympus\0\0\xCB";

void MakeTable(unsigned char * enc_table, unsigned short salt)
{
	unsigned long ss=salt;
	
	for (unsigned i=0;i<256;i++)
	{
		ss++;
		ss=((((ss<<6)+ss)&0xFFFFUL)%257)-1;
		enc_table[i]=(unsigned char)ss;
	}
}

void DoDecode(unsigned char * enc_table, unsigned char * buf, unsigned start, unsigned sz)
{
	for (unsigned pos=start;pos<sz;pos++)
		buf[pos]^=enc_table[pos&0xFF];
}

bool DoMcDecode(unsigned char  * buf, unsigned sz, bool encode)
{
	unsigned char enc[256];
	VerInfo & vi=*(VerInfo*)buf;

	if (sz<sizeof(VerInfo)) return false;

	if (memcmp(vi.sign,DAT_CopyRight,sizeof(DAT_CopyRight)))
		return false;
	bool sign= vi.pck == DAT_PACKED;
	if (encode == sign) return false; // already encoded/decoded

	MakeTable(enc, vi.salt);
	DoDecode (enc, buf, VerInfo_Plain, sz);	
	return true;
}
